package com.network.retrofit.utils;

import android.util.Log;

import com.network.retrofit.net.RetrofitClient;


/**
 * Created by 罗 on 16/3/29.
 * ＊功能：log管理
 */
public class NetWorkLogUtil {
    private static final int LOG_MAXLENGTH = 2000;
    

    public static void logD(final String tag, String message) {
        try {
            if (RetrofitClient.getService().getDebug()) {
                   if (Log.isLoggable(tag, Log.DEBUG)) {
                       Log.d(tag, message);
                   }
               }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void logD(final String tag, String message, Throwable cause) {
     if (RetrofitClient.getService().getDebug()) {
            if (Log.isLoggable(tag, Log.DEBUG)) {
                Log.d(tag, message, cause);
            }
        }
    }

    public static void logV(final String tag, String message) {
     if (RetrofitClient.getService().getDebug()) {
            if (Log.isLoggable(tag, Log.VERBOSE)) {
                Log.v(tag, message);
            }
        }
    }

    public static void logV(final String tag, String message, Throwable cause) {
     if (RetrofitClient.getService().getDebug()) {
            if (Log.isLoggable(tag, Log.VERBOSE)) {
                Log.v(tag, message, cause);
            }
        }
    }

    public static void logI(final String tag, String message) {
     if (RetrofitClient.getService().getDebug()) {
            Log.i(tag, message);
        }
    }

    public static void logI(final String tag, String message, Throwable cause) {
     if (RetrofitClient.getService().getDebug()) {
            Log.i(tag, message, cause);
        }
    }

    public static void logW(final String tag, String message) {
     if (RetrofitClient.getService().getDebug()) {
            Log.w(tag, message);
        }
    }

    public static void logW(final String tag, String message, Throwable cause) {
     if (RetrofitClient.getService().getDebug()) {
            Log.w(tag, message, cause);
        }
    }

    public static void logE(final String tag, String message) {
        try {
            if (RetrofitClient.getService().getDebug()) {
                showLogE(tag, message);
               }
        } catch (Exception e) {
            e.printStackTrace();
            
        }
    }

    public static void logE(final String tag, int message) {
        try {
            if (RetrofitClient.getService().getDebug()) {
                showLogE(tag, String.valueOf(message));
            }
        } catch (Exception e) {
            e.printStackTrace();
            
        }
    }

    public static void logE(String message) {
        try {
            if (RetrofitClient.getService().getDebug()) {
                showLogE("Debug", message);
            }
        } catch (Exception e) {
            e.printStackTrace();
            
        }
    }


    public static void logE(final String tag, String message, Throwable cause) {
        try {
            if(RetrofitClient.getService().getDebug()) {
                   Log.e(tag, message, cause);
               }
        } catch (Exception e) {
            e.printStackTrace();
            
        }
    }



    private static void showLogE(String tag, String message){
        try {
            if (RetrofitClient.getService().getDebug()) {
                String msg = message;
                int strLength = msg.length();
                int start = 0;
                int end = LOG_MAXLENGTH;

                if (strLength < end) {
                    Log.e(tag ,message);
                }else {
                    for (int i = 0; i < 100; i++) {
                        if (strLength > end) {
                            Log.e(tag + i, msg.substring(start, end));
                            start = end;
                            end = end + LOG_MAXLENGTH;
                        } else {
                            Log.e(tag + i, msg.substring(start, strLength));
                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        }
    }




}
